package com.heartOfGold;

/**
 * @author Greg Feigenson
 * @author Ryan Higgins
 * @version RC1
 *
 *
 * If you want to change the grammar of the conf files, edit the
 * getComponents method at the bottom. Be VERY careful because
 * there is no input checking in the conf parser for adding a line
 * and you may end up throwing more exceptions than you really want.
 */

/*
 Heart of Gold engine management system
 Copyright (C) 2004 Heart of Gold development team

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//
import com.heartOfGold.controller.Component;
import com.heartOfGold.model.megasquirtECU.MegasquirtMessenger;
import com.heartOfGold.view.Ui;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//import java.util.Enumeration;
//import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
//import gnu.io.*;



class Main {
    // private static ConfParser parser = ConfParser.getParser();


    /**
     * @param args
     * @throws java.io.IOException
     */
    public static void main(final String[] args) throws java.io.IOException {
        // Create the messenger, args[0] and args[2] are
        // the named pipes for talking to the C code
        MegasquirtMessenger theMessenger = MegasquirtMessenger.getMessenger(/*args[0],
                args[1]*/);

        // Create an array of HOG_Components, to add to the Heart Of Gold
        Component[] components = getComponents();

        // Add each of the HOG_Components as Observers of theMessenger
        for (int i = 0; i < components.length; ++i) {
            theMessenger.addObserver(components[i]);
        }

        // Create the main menu and pass the array of
        // HOG_Components as an argument
        Ui mainFrame = new Ui(components);

        // Lastly set the mainFrame visible and start the messaging
        mainFrame.setVisible(true);
        theMessenger.startMessaging();
    }

    /**
     * @return
     */
    public static Component[] getComponents() {
        Vector<String> v = new Vector<String>();
        // parser.setFile("hog.conf");
        // Hashtable table = parser.parse();
        // Enumeration hashKeys = table.keys();
        v.add("Help");
        v.add("IACV");
        v.add("Spark");
        v.add("Fuel");
        /*
         * while (hashKeys.hasMoreElements()) { String str =
         * (String)hashKeys.nextElement(); if (str.startsWith("panel")) {
         * v.add(table.get(str)); } }
         */
        /*
         * Since we're not quite sure how many components will actually be
         * listed in the conf we're just going to protect ourselves, just in
         * case every line is a component Don't want a polymorphic collection
         * because casting would waste time.
         */
        Component[] components = new Component[v.size()];
        Iterator<String> itr = v.iterator();
        int i = 0;
        while (itr.hasNext()) {
            try {
                Class<?> cls = Class.forName("com.heartOfGold.model." + (String) itr.next());
                Constructor<?>[] temp = cls.getConstructors();
                Component mhc = (Component) temp[0]
                        .newInstance();
                components[i] = mhc;
                i++;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        Component[] mhcArray = new Component[i];
        System.arraycopy(components, 0, mhcArray, 0, i);

        // Reduce the array only to the elements we want
        return mhcArray;
    }

}
